#include <REGX52.H>
#include "delay.h"

#define  DEV   0
#define  TEST  1

#define T1MS_1200bps   0xE8;     /* (e8,-24,SMOD=0) @4800bps  pcon&=0x7f @11.0592MHz*/
#define T1MS_2400bps   0xF4;     /* (f4,-12,SMOD=0) @4800bps  pcon&=0x7f */
#define T1MS_4800bps   0xFA;     /* (fa, -6,SMOD=0) @4800bps  pcon&=0x7f */
#define T1MS_9600bps   0xFD;     /* (fd, -3,SMOD=0) @9600bps  pcon&=0x7f */
#define T1MS_19k2bps   0xfD;     /* (fd, -3,SMOD=1) @19.2kbps pcon|=0x80 */

sbit csda=P3^2;						//DA
sbit wr51=P3^6;           
sbit adcs=P2^4;            //AD
sbit rd51=P3^7;
//sbit wela=P2^7;
//sbit dula=P2^6;						  //数码管
unsigned char start=1;
unsigned char wave[100]={136,144,152,160,168,175,182,190,197,
	203,210,216,221,227,232,236,240,244,247,250,252,
	254,255,256,256,256,255,254,252,250,247,244,240,
	236,232,227,221,216,210,203,197,190,183,175,168,
	160,152,144,136,128,120,112,104,96,89,81,74,67,
	60,53,47,41,35,30,25,20,16,12,9,6,4,2,1,0,0,0,1,
	2,4,6,9,12,16,20,24,29,34,40,46,52,59,66,73,81,
	88,96,104,112,120,128};   //正弦波
unsigned char num=0,len=100;      //正弦波数组长度

unsigned char message=0;  //串口接收
	
void dac_config();
void sel_bps(unsigned char sel);
void adc_config();	
void uart_config();
void send_char(unsigned char datas);


/*********************************
函数名：sel_bps(select bps)
功能:设置串口波特率
sel为选择通讯速率：
  0=1200,1=2400,2=4800,3=9600,4=19.2k
*********************************/
void sel_bps(unsigned char sel)
{
     switch(sel)
     {
        case 0:PCON&=0x7f;
               TH1=T1MS_1200bps;   /* T1 use sio */
               TL1=T1MS_1200bps;
               break;
        case 1:PCON&=0x7f;
               TH1=T1MS_2400bps;   /* T1 use sio */
               TL1=T1MS_2400bps;
               break;
        case 2:PCON&=0x7f;
               TH1=T1MS_4800bps;   /* T1 use sio */
               TL1=T1MS_4800bps;
               break;
        case 3:PCON&=0x7f;
               TH1=T1MS_9600bps;   /* T1 use sio */
               TL1=T1MS_9600bps;
               break;
        case 4:PCON|=0x80;
               TH1=T1MS_19k2bps;   /* T1 use sio */
               TL1=T1MS_19k2bps;
               break;
     }

}

/*
 *输出正弦函数
 */
#if DEV
void main(){
	unsigned char adval=0;
	IE=0x85;TCON=0x05;PX1=1;//开中断，设置INT1为高优先级
	dac_config();
	adc_config();
	uart_config();
	send_char(0x0F);
	while(1){
		rd51=0;                     //AD读使能
		adcs=0;
		adval=P0;
//		send_char(adval);
		send_char(num);
		rd51=1; 
		adcs=1;
		delay10ms();                //采样时间间隔
		P1=~P1;
	}
}
#endif

#if TEST
void main(){
	unsigned char adval=0;
	IE=0x85;TCON=0x05;PX1=1;//开中断，设置INT1为高优先级
	dac_config();
	adc_config();
	uart_config();
	while(1){
		if(num>=len){ 
			 num = 0;
		}
		P0=wave[num];
		//send_char(num);
		send_char(P0);
		csda=0;
		wr51=0;
		//wr51=1;
		//csda=1;
		num++;
		Delay10ms();                //采样时间间隔
	}
}
#endif


/*
 * dac初始化
 */
void dac_config(){
//	wela=0;
//  dula=0;
  csda=1; 
	wr51=1;
}

/*
 * adc初始化
 */
void adc_config(){
//	wela=0;
//  dula=0;
  adcs=1;
	wr51=1;
	
}

/*
 * 串口初始化
 */
void uart_config(){
	PS=1;
	TMOD=(0x0F&TMOD)|0x20; //T0 set Mode 1
	sel_bps(3); // 9600 bps
	TR1=1;      //启动定时器
	//设置工作方式
	SM0=0;
	SM1=1;
	REN=1; //允许串口接收
	EA=1;
	ES=1;
}



/*
 * 串口发送数据
 */
void send_char(unsigned char datas){
  SBUF=datas;             // 发送数据
	while(!TI);
	TI=0;				      // 等待数据发送完毕
}



